package com.yidu.hbatsams.action.TrainingPlan.zjj;


import com.yidu.hbatsams.domain.AtsBaseSport;
import com.yidu.hbatsams.domain.AtsEmpCoach;
import com.yidu.hbatsams.domain.AtsPlanCurriculum;
import com.yidu.hbatsams.domain.AtsPlanItem;
import com.yidu.hbatsams.service.AtsBaseSportService;
import com.yidu.hbatsams.service.emp.AtsEmpCoachService;
import com.yidu.hbatsams.service.TrainingPlan.AtsPlanItemService;
import com.yidu.hbatsams.service.TrainingPlan.zjj.PlanCurriculumService;
import com.yidu.hbatsams.utils.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @program: hbats
 * @description
 * @author: 周佳佳
 * @create: 2019-12-10 23:03
 **/
@Controller
@RequestMapping("planCurriculumMapperController")
public class PlanCurriculumMapperController {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    PlanCurriculumService planCurriculumService;
    @Resource
    AtsPlanItemService atsPlanItemService;

    @Resource
    AtsBaseSportService atsBaseSportService;

    @Resource
    AtsEmpCoachService atsEmpCoachService;

    @RequestMapping("curricuAddOrupdate")
    @ResponseBody
    public Message curricuAddOrupdate(AtsPlanCurriculum planCurriculum, @RequestParam(value = "numArr[]", required = false) String[] numArr, HttpSession session) {
        Message message = new Message();
        Integer weekId = (Integer) session.getAttribute("weekId");
        if (weekId == null) {
            message.setNum(0);
            message.setMsg("请先保存周计划！");
        } else if (StringUtils.isEmpty(planCurriculum.getWeekId())) {
            planCurriculum.setWeekId(weekId);
        }
        if (planCurriculum.getCurriculumId() != null && !"".equals(planCurriculum.getCurriculumId())) {
            //有课id就是修改
            planCurriculumService.updatePlancurricu(planCurriculum, numArr);
            message.setNum(2);
        } else {
            //没有课id就是增加
            planCurriculumService.insertSelectiveFour(planCurriculum, numArr);
            message.setNum(1);
        }
        return message;
    }

    /**
     * 查询课计划名字是否存在
     *
     * @param atsPlanCurriculum
     * @return
     */
    @ResponseBody
    @RequestMapping("selectCurriculumName")
    public int selectCurriculumName(AtsPlanCurriculum atsPlanCurriculum, HttpSession session) {
        System.out.println("进来了");
        Integer weekId = (Integer) session.getAttribute("weekId");
        System.out.println(weekId + "asd");
        atsPlanCurriculum.setWeekId(weekId);
        int countAll = planCurriculumService.countFive(atsPlanCurriculum);
        return countAll;
    }

    @RequestMapping("selectCurricu")
    @ResponseBody
    public List<AtsPlanCurriculum> selectCurricu(HttpSession session, Integer weekId) {
        //weekId为空是增加 不为空就是编辑
        if (weekId == null) {


            weekId = (Integer) session.getAttribute("weekId");

            if (weekId == null) {
                return null;
            }
        }
        List<AtsPlanCurriculum> list = planCurriculumService.selectCurricu(weekId);
        return list;
    }

    @RequestMapping("findByCur")
    @ResponseBody
    public AtsPlanCurriculum findByCur(int curriculumId) {
        AtsPlanCurriculum planCurriculum = planCurriculumService.findById(curriculumId);
        return planCurriculum;
    }

    @RequestMapping("selectWeekcount")
    @ResponseBody
    public int selectWeekcount() {
        return planCurriculumService.selectWeekcount();
    }

    @RequestMapping("selectFindIsvaTwo")
    @ResponseBody
    public AtsPlanCurriculum selectFindIsvaTwo(Integer weekId) {
        AtsPlanCurriculum atsPlanCurriculum = planCurriculumService.selectByIsvaTwo(weekId);
        return atsPlanCurriculum;
    }

    @RequestMapping("updateZancunIsva")
    @ResponseBody
    public Message updateZancunIsva(AtsPlanCurriculum atsPlanCurriculum) {
        Message message = new Message();
        int rows = planCurriculumService.updateZancunIsva(atsPlanCurriculum);
        if (rows > 0) {
            message.setNum(1);
        }
        return message;
    }

    @RequestMapping("selectMaxcurruculum")
    @ResponseBody
    public AtsPlanCurriculum atsPlanCurriculumt(HttpSession session) {
        Integer weekId = (Integer) session.getAttribute("weekId");
        AtsPlanCurriculum atsPlanCurriculum = planCurriculumService.selectMaxcurruculum(weekId);
        return atsPlanCurriculum;
    }

    /**
     * 删除课计划 然后删除它下面的指标
     *
     * @param curriculumId
     * @return
     */
    @RequestMapping("updateCurriculumId")
    @ResponseBody
    public Message updateCurriculumId(Integer curriculumId) {
        Message message = new Message();
        AtsPlanCurriculum atsPlanCurriculum = new AtsPlanCurriculum();
        atsPlanCurriculum.setIsva(0);
        atsPlanCurriculum.setCurriculumId(curriculumId);
        int rows = planCurriculumService.updateIsva(atsPlanCurriculum);
        AtsPlanItem atsPlanItem = new AtsPlanItem();
        atsPlanItem.setPlandate("1");
        atsPlanItem.setIsva(0);
        atsPlanItem.setPid(curriculumId);
        atsPlanItemService.updatecurriculumItem(atsPlanItem);
        if (rows > 0) {
            message.setNum(1);
        }
        return message;
    }

    @ResponseBody
    @RequestMapping("findCoachexp")
    public AtsEmpCoach findCoachexp(int coachId) {
        AtsEmpCoach atsEmpCoach = atsEmpCoachService.selectexpert(coachId);
        return atsEmpCoach;
    }

    @ResponseBody
    @RequestMapping("findCoachexpone")
    public AtsBaseSport selectByPrimaryKey(int sportId) {
        AtsBaseSport atsBaseSport = atsBaseSportService.selectByPrimaryKey(sportId);
        return atsBaseSport;
    }
}
